package comp6231.project.replica.thomas;

import java.io.*;

public class FileOperation {
	
	private String customerId;
	private String itemId;
	private int numberOfItem;
	private String currentContent;
	
	FileOperation(String content)
	{
		currentContent = content;
		
	}
	
	
	public void writeFile(String customerID, String itemID, int numberOfItem) throws IOException
	{
		try{
			String filePath = "customerlog";
			filePath +="/"+currentContent+customerID+".txt";
		FileWriter fw = new FileWriter(filePath,true);
		BufferedWriter bw = new BufferedWriter(fw);
		
		bw.write("--------customer bought record---------");
		bw.newLine();
		bw.write("customerId: ");
		bw.newLine();
		bw.write(customerID);
		
		bw.newLine();
		bw.write("itemId: ");
		bw.newLine();
		bw.write(itemID);
		
	
		bw.newLine();
		bw.write("purchase number of item: ");
		bw.newLine();
		bw.write(String.valueOf(numberOfItem));
		bw.newLine();
		bw.write("-------end customer bought record------");
		bw.newLine();
		bw.newLine();
		bw.newLine();
		
		bw.close();
		fw.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * This method used for change the transaction amount of item for the specific customer
	 * @param customerID
	 * @param itemID
	 * @param numberOfItem amount that to be return
	 */
	public void changeCustomerInfo(String customerID, String itemID, int numberOfItem)
 {
		String formerPurchaseNumber = "";
		String temp ="";
		try {
			String filePath = "customerlog";
			filePath +="/"+currentContent+customerID+".txt";
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();
			


			for (int j = 1; (temp = br.readLine()) != null; j++) {
	
				if(temp.equals(itemID)){
					System.out.println("the customer has found");
					buf = buf.append(temp);
					buf = buf.append(System.getProperty("line.separator"));
					buf = buf.append(br.readLine());
					buf = buf.append(System.getProperty("line.separator"));
					//file data to be modified
					formerPurchaseNumber = br.readLine();
					buf = buf.append(Integer.parseInt(formerPurchaseNumber)-numberOfItem);
					System.out.println("Earlier, the client bought : " +formerPurchaseNumber+" number of"+itemID);
					System.out.println("Now, the client return : "+ numberOfItem+" number of"+itemID);
					break;				
					
				}
				
				
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.separator"));
				

			}
           
	        while ((temp = br.readLine()) != null) {
                buf = buf.append(System.getProperty("line.separator"));
                buf = buf.append(temp);
            }

	        br.close();
	        FileOutputStream fos = new FileOutputStream(file);
	        
	        BufferedWriter newBw = new BufferedWriter(new FileWriter(filePath));
	        newBw.write(buf.toString().toCharArray());
	        newBw.close();
	        
	        isr.close();
	        fis.close();
	        fos.close();
	        
           
           
		  }
		  catch(IOException e){
			  e.printStackTrace();
		  }
	}
	
	
	/**
	 * this method used for get the item Amount
	 * @param customerID
	 * @param itemID
	 * @return
	 */
	
	
	public int getTransactionAmount(String customerID, String itemID){
		String temp ="";
		String formerPurchaseNumber = "";
		int itemAmount = 0;
		try{
			String filePath = "customerlog";
			filePath +="/"+currentContent+customerID+".txt";
		File file = new File(filePath);
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		StringBuffer buf = new StringBuffer();
		
		for (int j = 1; (temp = br.readLine()) != null; j++) {
			
			if(temp.equals(itemID)){
				System.out.println("the customer has found");
				br.readLine();
				itemAmount =  Integer.valueOf(br.readLine());
				break;
				
			}
			

		}
		
		br.close();
		
		return itemAmount;
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		
		return 0;
	}
	
	
	
	public void readCustomerInfo(String customerID) throws IOException{
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("customer.txt")));
			String data = null;
		
			while((data = br.readLine())!=null) 
			{
				if(data == customerID){
					setItemId(br.readLine());
					setNumberOfItem(Integer.parseInt(br.readLine()));
					break;
				}				
			}
			br.close();
			
		
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
		
	}
	
	

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public int getNumberOfItem() {
		return numberOfItem;
	}

	public void setNumberOfItem(int numberOfItem) {
		this.numberOfItem = numberOfItem;
	}
	
	
}
